<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\library\Sms;
use fast\Random;
use think\Validate;

class Login extends Api{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 会员登录
     * @param string $account  账号
     * @param string $password 密码
     */
    public function login()
    {
        $account = $this->request->request('mobile');
        $password = $this->request->request('password');

        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        if(!isMobilePhone($account)){
            $this->error(__('Please input the correct mobile number'));
        }
        $ret = $this->auth->login($account, $password);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 手机验证码登录
     *
     * @param string $mobile  手机号
     * @param string $captcha 验证码
     */
    public function mobilelogin()
    {
        $mobile = $this->request->request('mobile');
        $captcha = $this->request->request('captcha');
        if (!$mobile || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!isMobilePhone($mobile)) {
            $this->error(__('Mobile is incorrect'));
        }
        if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
            $this->error(__('Captcha is incorrect'));
        }
        $user = \app\common\model\User::getByMobile($mobile);
        if ($user) {
            if ($user->status != '1') {
                $this->error(__('Account is locked'));
            }
            //如果已经有账号则直接登录
            $ret = $this->auth->direct($user->id);
        } else {
            $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
        }
        if ($ret) {
            Sms::flush($mobile, 'mobilelogin');
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 注册会员
     * @param string $mobile   手机号
     * @param string $code   验证码
     */
    public function register()
    {
        $mobile = $this->request->request('mobile');
        $code = $this->request->request('code');
        $post = request()->post();
        $validate = new Validate([
            'mobile'        => 'require|length:11,11',
            'code'          => 'require',
            'surname'      => 'require',
            'nickname'      => 'require',
            'gender'        => 'require|in:0,1'
        ],[],[
            'mobile'        =>'手机号',
            'code'          =>'验证码',
            'surname'       =>'姓',
            'nickname'      =>'名',
            'gender'        => '性别'
        ]);
        if (!$validate->check($post)) {
            $this->error($validate->getError());
        }
        if ($mobile && !isMobilePhone($mobile)) {
            $this->error(__('Mobile is incorrect'));
        }
        $ret = Sms::check($mobile, $code, 'register');
        if (!$ret) {
            $this->error(__('Captcha is incorrect'));
        }
        $ret = $this->auth->register($mobile, request()->post());
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Sign up successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }


    /**
     * 注销登录
     */
    public function logout()
    {
        $this->auth->logout();
        $this->success(__('Logout successful'));
    }
}